<?php
/**
 * Pertyler Classes
 * User Class
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

class User extends Client
{
	protected $id;

	protected function __construct($id)
	{
		parent::__construct();

		$this->id = $id;
	}

	public static function Get()
	{
		if((empty($_SESSION['Id'])) OR (empty($_SESSION['Password'])))
			return NULL;

		if(!(parent::Get() instanceof self))
			new self($_SESSION['Id']);

		return parent::Get();
	}

	final public function Id()
	{
		return $this->id;
	}

	public function __ToString()
	{
		return $this->id;
	}

	protected function password()
	{
		return $_SESSION['Password'];
	}

	protected static function passwordGeneralAvailability($password)
	{
		if(strlen($password) < 4)
			return false;

		return true;
	}

	protected function passwordAvailability($password)
	{
		if(!self::passwordAvailability($password))
			return false;

		if($password == $this->password())
			return false;

		return true;
	}

	public function SelectLanguage()
	{
		if(empty($_SESSION['Language']))
			$_SESSION['Language'] = parent::SelectLanguage()->Id();

		return Language::Select($_SESSION['Language']);
	}

	public static function Login($id,
			$password)
	{
		if(!self::passwordGeneralAvailability($password))
			return false;

		$_SESSION['Id'] = (string) $id;

		$_SESSION['Password'] = md5($password);

		return true;
	}

	public function Logout()
	{
		unset($_SESSION['Id']);

		unset($_SESSION['Password']);

		unset($this);

		return true;
	}
}